package com.bgs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bgs.pojo.Dormitory;
import com.bgs.pojo.Grade;
import com.bgs.pojo.StudentDormitoryRelation;
import com.bgs.pojo.studentGradeRelation;
import com.bgs.pojo.vo.*;
import com.bgs.service.DormitoryService;
import com.bgs.utils.MyContent;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 梁玉杰
 * @Date: 2021/8/12 16:21
 * @Description: TODO
 */
@Controller
public class DormitoryController {

    Logger log = LogManager.getLogger();

    @Autowired
    private DormitoryService dormitoryService;

    @RequestMapping("/findAllDoc")
    @ResponseBody
    public Map<String,Object> findAllDoc(@RequestBody DormitoryVo dormitoryVo){

        log.info("模糊查询的"+dormitoryVo);

        Map<String, Object> m = new HashMap<>();

        //设置分页信息(当前页码，每页显示数量)
        PageHelper.startPage(dormitoryVo.getPage(),dormitoryVo.getLimit());

        //调用Service查询宿舍
        List<Dormitory> d = dormitoryService.findAll(dormitoryVo);

        //把全查回来的宿舍集合，每个的宿舍名称再拿出来查询当前宿舍的现住学生
        for (int i = 0; i < d.size(); i++) {

            log.info("总数"+d.size());
            log.info(d.get(i).getDormitoryName());
            //当前宿舍的现住学生
          Integer number = dormitoryService.number(d.get(i).getDormitoryName());

          d.get(i).setDorstuNumber(number+"(已用)/"+d.get(i).getBedNumber()+"(总数)");

        }

//        for (Dormitory dor : d) {
//           log.info(dor.getDormitoryName());
//            //当前宿舍的现住学生
//            Integer number = dormitoryService.number(dor.getDormitoryName());
//
//            dor.setDorstuNumber(number+"(已用)/"+dor.getBedNumber()+"总数");
//
//        }


        //创建分页对象
        PageInfo<Dormitory> dor = new PageInfo<>(d);
        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,dor.getTotal());
        m.put(MyContent.DATA,dor.getList());

        return m;
    }


    //区域下拉框
    @RequestMapping("dorxlkone")
    @ResponseBody
    public List<Dormitory> dorxlkone(){
        return dormitoryService.dorxlkone();
    }

    //楼号下拉框
    @RequestMapping("dorxlktwo")
    @ResponseBody
    public List<Dormitory> dorxlktwo(@RequestBody Dormitory d){

        log.info("楼号下拉框"+d);
        return dormitoryService.dorxlktwo(d);
    }


    //楼层下拉框
    @RequestMapping("dorxlkthree")
    @ResponseBody
    public List<Dormitory> dorxlkthree(@RequestBody Dormitory d){

        log.info("楼层下拉框"+d);

        List<Dormitory> dorxlkthree = dormitoryService.dorxlkthree(d);

        log.info("楼层下拉框的楼层"+dorxlkthree.size());
        log.info(dorxlkthree);
//        for (int i = 0; i <dorxlkthree.size() ; i++) {
//            System.out.println("楼层下拉框的楼层"+dorxlkthree.get(i).getBuildingFloor());
//        }

        return dorxlkthree;
    }

    //管理员下拉框
    @RequestMapping("dorxlkgly")
    @ResponseBody
    public List<Dormitory> dorxlkgly(){

        return dormitoryService.dorxlkgly();
    }

    //床位数量下拉框
    @RequestMapping("dorxlkcwsl")
    @ResponseBody
    public List<Dormitory> dorxlkcwsl(){

        return dormitoryService.dorxlkcwsl();
    }

    //使用情况下拉框
    @RequestMapping("dorxlksyzt")
    @ResponseBody
    public List<Dormitory> dorxlksyzt(){

        return dormitoryService.dorxlksyzt();
    }


    /**
     * @Author 梁玉杰
     * @Description //添加宿舍的
     * @Date 2021/8/14 13:10
     * @Param DandStu 是宿舍和学生和宿管老师的名字的包装类的包装类
     * @return
     **/
    @RequestMapping("/addDor")
    @ResponseBody
    public Map<String,Object> addDor(@RequestBody DandStu d){

        log.info("添加宿舍的"+d);

        Dormitory dor = new Dormitory();
        dor.setArea(d.d.getArea());
        dor.setBuildingNo(d.d.getBuildingNo());
        dor.setBuildingFloor(d.d.getBuildingFloor());
        dor.setDormitoryName(d.d.getDormitoryName());
        dor.setBedNumber(d.d.getBedNumber());
        dor.setDormSupervisorId(d.d.getDormSupervisorId());
        dor.setDormSupervisor(d.getDormSupervisor());
        dor.setUseStatus(d.d.getUseStatus());

        Map<String, Object> m = new HashMap<>();

        if(dormitoryService.insertSelective(dor) > 0){

                //使用主键的自增ID来修改宿舍ID
                Integer pkId = dor.getPkId();
                Dormitory pdor = new Dormitory();
                pdor.setPkId(pkId);
                pdor.setDormitoryId(pkId.toString());
                if(dormitoryService.updateByPrimaryKeySelective(pdor) > 0){
                    log.info("添加的修改成功！！");
                }
            m.put("success",true);
            m.put("message","添加成功");
        }else{
            m.put("success",false);
            m.put("message","添加失败");
        }
        return m;
    }

  /**
     * @Author 梁玉杰
     * @Description //修改宿舍的
     * @Date 2021/8/14 13:10
     * @Param DandStu 是宿舍和学生和宿管老师的名字的包装类的包装类
     * @return
     **/
    @RequestMapping("/upaDor")
    @ResponseBody
    public Map<String,Object> upaDor(@RequestBody DandStu d){

        log.info("添加宿舍的"+d);

        Dormitory dor = new Dormitory();
        dor.setArea(d.d.getArea());
        dor.setBuildingNo(d.d.getBuildingNo());
        dor.setBuildingFloor(d.d.getBuildingFloor());
        dor.setDormitoryName(d.d.getDormitoryName());
        dor.setBedNumber(d.d.getBedNumber());
        dor.setDormSupervisorId(d.d.getDormSupervisorId());
        dor.setDormSupervisor(d.getDormSupervisor());
        dor.setUseStatus(d.d.getUseStatus());
        dor.setPkId(d.d.getPkId());


        Map<String, Object> m = new HashMap<>();
        if(dormitoryService.updateByPrimaryKeySelective(dor) > 0){
            m.put("success",true);
            m.put("message","修改成功");
        }else{
            m.put("success",false);
            m.put("message","修改 失败");
        }

        return m;
    }


    /**
     * 删除宿舍
     */
    @RequestMapping("/dordeleteBypkId")
    @ResponseBody
    public Map<String, Object> dordeleteBypkId(@RequestBody Integer pkId){
        Map<String,Object> m = new HashMap<>();
        if(dormitoryService.deleteById(pkId)>0){
            m.put("success",true);
            m.put("message","删除成功");
        }else{
            m.put("success",false);
            m.put("message","删除失败");
        }
        return m;
    }

    /**
     * 批量删除宿舍
     */
    @RequestMapping("/dorbatchDelete")
    @ResponseBody
    public Map<String, Object> dorbatchDelete(@RequestBody String ids){
        System.out.println(ids);
        Map<String,Object> map = new HashMap<>();
        int count = 0;
        JSONObject jsonObject = JSONObject.parseObject(ids);
        JSONArray arrId = jsonObject.getJSONArray("ids");

//        JSONArray arrId = JSONArray.parseArray(ids);

        log.info("arrId::: " + arrId);
        for (int i = 0; i < arrId.size(); i++) {
            log.info("批量删除"+arrId.getInteger(i));
            count = dormitoryService.deleteById(arrId.getInteger(i));
            if(count>0){
                map.put("success",true);
                map.put("message","删除成功");
            }
        }
        //判断受影响行数是否为0
        if(count<=0){
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }


    //详情功能的宿舍添加学生信息
    @RequestMapping("/doraddStu")
    @ResponseBody
    public Map<String, Object> doraddStu(@RequestBody DandStu g){
//        JSONObject cinfor1 = JSONObject.parseObject(cinfor);
//        System.out.println(cinfor1.getString("classTeacherName"));

        Map<String, Object> m = new HashMap<>();
        System.out.println("宿舍添加学生的"+g);
//        System.out.println("添加学生的"+s);
//        System.out.println(stu);
        StudentDormitoryRelation sD = new StudentDormitoryRelation();
        sD.setStudentId(g.stu.getStuId().toString());  //学生的Id
        sD.setDormitoryDormitoryId(g.d.getDormitoryId());  //宿舍的ID
        sD.setDormitoryDormitoryName(g.d.getDormitoryName());  //宿舍名称


        if(dormitoryService.insertDS(sD)>0){
            m.put("success",true);
            m.put("message","添加成功");
        }else{
            m.put("success",false);
            m.put("message","添加失败");
        }

        return m;
    }


    //详情功能的宿舍批量添加学生信息
    @RequestMapping("dorbatchaddStu")
    @ResponseBody
    public Map<String,Object> dorbatchaddStu(@RequestBody String s){

        JSONObject jsonObject = JSONObject.parseObject(s);
        String dormitoryId = jsonObject.getString("dormitoryId");       //宿舍ID
        String dormitoryName = jsonObject.getString("dormitoryName");   //宿舍名称
        JSONArray arrId = jsonObject.getJSONArray("stuId");     //要添加的学生数组

        StudentDormitoryRelation sD = new StudentDormitoryRelation();
        sD.setDormitoryDormitoryId(dormitoryId);  //宿舍的ID
        sD.setDormitoryDormitoryName(dormitoryName);  //宿舍名称
        int count = 0;
        for (int i = 0; i < arrId.size(); i++) {
            sD.setStudentId(arrId.getInteger(i).toString());  //学生的Id
            if (dormitoryService.insertDS(sD) > 0){
                count++;
            }
        }
        HashMap<String, Object> m = new HashMap<>();

        if(count>0){
            m.put("success",true);
            m.put("message","添加成功");
        }else{
            m.put("success",false);
            m.put("message","添加失败");
        }

        return m;
    }


    //详情功能的展示本班学生信息的
    @RequestMapping("/dorfindAllds")
    @ResponseBody
    public Map<String,Object> dorfindAllds(@RequestBody StudentDormitoryRelationVo sd){

        log.info("模糊查询的"+sd);

        Map<String,Object> m = new HashMap<>();
        //设置分页信息(当前页码，每页显示数量)
        PageHelper.startPage(sd.getPage(),sd.getLimit());
        StudentDormitoryRelation stuDor = new StudentDormitoryRelation();
        BeanUtils.copyProperties(sd,stuDor);
        log.info("模糊查询"+stuDor);
        //调用service查询班级方法
        List<StudentDormitoryRelation> l = dormitoryService.dorfindAllds(stuDor);
        //创建分页对象
        PageInfo<StudentDormitoryRelation> gradePageInfo = new PageInfo<>(l);
        log.info("PageInfo<StudentDormitoryRelation>"+gradePageInfo);
        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,gradePageInfo.getTotal());
        m.put(MyContent.DATA,gradePageInfo.getList());

        return m;
    }

    /*
    删除宿舍学生
     */
    @RequestMapping("/ddordeleteByIdSstu")
    @ResponseBody
    public Map<String, Object> ddordeleteByIdSstu(@RequestBody Integer pkId){
        Map<String,Object> m = new HashMap<>();
        if(dormitoryService.deleteByIdSstu(pkId)>0){
            m.put("success",true);
            m.put("message","删除成功");
        }else{
            m.put("success",false);
            m.put("message","删除失败");
        }
        return m;
    }


    /**
     * 批量删除宿舍的学生
     */
    @RequestMapping("/ddordeleteBySstuID")
    @ResponseBody
    public Map<String, Object> ddordeleteBySstuID(@RequestBody String ids){
        System.out.println(ids);
        Map<String,Object> map = new HashMap<>();
        int count = 0;
        JSONObject jsonObject = JSONObject.parseObject(ids);
        JSONArray arrId = jsonObject.getJSONArray("ids");

//        JSONArray arrId = JSONArray.parseArray(ids);

        log.info("arrId::: " + arrId);
        for (int i = 0; i < arrId.size(); i++) {
            log.info("批量删除"+arrId.getInteger(i));
            count = dormitoryService.deleteByIdSstu(arrId.getInteger(i));
            if(count>0){
                map.put("success",true);
                map.put("message","删除成功");
            }
        }
        //判断受影响行数是否为0
        if(count<=0){
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }



}
